/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import ejb.Calendarejb;
import ejb.Userejb;
import entity.User;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author user
 */
@Named
@RequestScoped

public class Registrationbean implements Serializable{
    
    @Inject
    private Userejb userejb;
    @Inject
    private Calendarejb calejb;
    
    private User user;
    
    private String username;
    
    public Registrationbean(){
        
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String isRegistered(){
        username = user.getUsername();     
        if(userejb.checkRegistered(username)==null){
                userejb.save(user);
                calejb.buildCalendar(user); //chiama ejb e salva calendar con nome user.username nel campo owner
                return "/login?faces-redirect=true";     
                
        }
        else{
            return "/errorregistered?faces-redirect=true";
        }
    }
    
}
